package com.jcl.recruit.api.common.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.recruit.common.service.CommonService;
import com.jcl.recruit.config.model.Config;
import com.jcl.recruit.config.model.PullDownConfig;
import com.jcl.recruit.config.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

/**
 * @author 刘明新
 * @date 2018/8/2 上午11:43
 */
@RestController
@RequestMapping("/api/config")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    /**
     * 查找所有config信息
     * @return
     */
    @RequestMapping("/find")
    public Map<String,Object> findConfig(){
        Map<String,Object> map = new LinkedHashMap<>();
        PageHelper.startPage(1,DEFAULT_PAGE_SIZE);
        List<Config> configList = configService.selectAllInfos();
        PageInfo pageInfo = new PageInfo(configList);
        map.put("type","success");
        map.put("message","执行成功");
        map.put("configs",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    /**
     * 只修改信息，不新增
     * @param config
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Map<String,Object> save(@RequestBody Config config){
        Map<String,Object> map = new LinkedHashMap<>();
        if(config == null || config.getId() == null || configService.selectPrimaryKey(config.getId()) == null){
            map.put("type","error");
            map.put("message","执行失败");
            map.put("error_info","id不能为空或者id不存在");
            return map;
        }
        int result = configService.update(config);
        map.put("type",result == 1?"success":"error");
        map.put("message",result == 1?"执行成功":"执行失败");
        map.put("config",configService.selectPrimaryKey(config.getId()));
        return map;
    }

    /**
     * 前台配置下拉选项
     * @return
     */
    @RequestMapping("/getall")
    public Map<String,Object> configDetall(){
        Map<String,Object> map = new LinkedHashMap<>();
        List<Config>  configs = configService.selectAllInfos();
        Map<String,Object> objectMap = new HashMap<>();
        for(Config config:configs){
            if(config.getConfigkey().equals("interview_department") || config.getConfigkey().equals("interview_place"))
                objectMap.put(config.getConfigkey(), CommonService.splitString(config.getDatas(),"\\|"));
        }
        List<PullDownConfig> pullDownConfigs = configService.selectAllConfigs();
        for(PullDownConfig entity:pullDownConfigs){
            List<String> list = CommonService.splitString(entity.getDatas(),"\\|");
            objectMap.put(entity.getConfigName(),list);
            Map<String,String> stringMap = new LinkedHashMap<>();
            for(String str:list){
                if(str.contains(":")){
                    stringMap.put(str.split(":")[0],str.split(":")[1]);
                }
            }
            if(stringMap.size() > 0) {
                objectMap.put(entity.getConfigName(), stringMap);
            }
        }
        map.put("configs",objectMap);
        map.put("message","执行成功");
        map.put("type","success");
        return map;
    }
}
